package GUI;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.util.Vector;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SpringLayout;
import DataObjects.ConstantObjects;
import DataObjects.Table;


/**
 * this class creates the JPanel for scheduling tables 
 * into the restaurant. 
 * 
 * @author igor
 *
 */
public class PaintSchedulePanel extends JPanel implements MouseMotionListener,MouseListener {

	
	private static final long serialVersionUID = 1L;

	MainMenu mainMenu;
	
	private int  quantity2=new ConstantObjects().getQuantity_of_2_persons_table();
	private int  quantity4=new ConstantObjects().getQuantity_of_4_persons_table();
	
	private int table2X=20 ;
	private int table2Y=100;
	
	private int table4X=20 ;
	private int table4Y=200;
	
	private int table6X=20 ;
	private int table6Y=300;
	
	private int table8X=20 ;
	private int table8Y=400;
	
	private int table10X=20 ;
	private int table10Y=500;
	
	
	private int tableSize=0;
	private int tableToRemove=-1;
	private int tableToTurn=-1;
	private Table tableForChange;
	private int frameCoordX=0;
	private int frameCoordY=0;
	
    private ImageIcon imtable2_1;
    private ImageIcon imTable4_1;
    private ImageIcon imTable6_1;
    private ImageIcon imTable8_1;
    private ImageIcon imTable10_1;
    private ImageIcon imtable2_2;
    private ImageIcon imTable4_2;
    private ImageIcon imTable6_2;
    private ImageIcon imTable8_2;
    private ImageIcon imTable10_2;
    ImageIcon background = new ImageIcon("tarshim.png");

	
	private Image tableImg2_1;
	private Image tableImg4_1;
	private Image tableImg6_1;
	private Image tableImg8_1;
	private Image tableImg10_1;
	private Image tableImg2_2;
	private Image tableImg4_2;
	private Image tableImg6_2;
	private Image tableImg8_2;
	private Image tableImg10_2;
	private Vector<Table> vecTables =new Vector<Table>();
	
	
	private MyJPopupMenu Pmenu;
	private MyJPopupMenu PmenuForMovedTables;
	private Font f = new Font("Serif",Font.BOLD,16);
	
	private int tableNumber=0;
	private Dimension dim;
	
	
	
	
	
	
	
	
	
	/**
	 * creates the panel that will move and schedule the table 
	 * @param mm - main data of the programm
	 */
	public PaintSchedulePanel(MainMenu mm)
	{
		this.mainMenu=mm;
		this.addMouseMotionListener(this);
		this.addMouseListener(this);
		setPreferredSize(getMaximumSize());
		Pmenu=new MyJPopupMenu();
        Pmenu.add(getJMenuItemNewTable());
        PmenuForMovedTables=new  MyJPopupMenu();
        PmenuForMovedTables.add(getJMenuItemNewTable());
        PmenuForMovedTables.add(getJMenuItemRemoveTable());
        PmenuForMovedTables.add(getJMenuItemChangeTable());
        PmenuForMovedTables.add(getJMenuItemTurnTable());
		this.addMouseListener(new RightClickerListener());
		initialize();				 	         			 
	}
	
	
	
	/**
	 * creates all necessary attributes for the panel 
	 */
	private void initialize()
	{
		imtable2_1=new ImageIcon("table2_1.png");
	    imTable4_1=new ImageIcon("table4_1.png");
	    imTable6_1=new ImageIcon("table6_1.png");
	    imTable8_1=new ImageIcon("table8_1.png");
	    imTable10_1=new ImageIcon("table10_1.png");
	    
	    imtable2_2=new ImageIcon("table2_2.png");
	    imTable4_2=new ImageIcon("table4_2.png");
	    imTable6_2=new ImageIcon("table6_2.png");
	    imTable8_2=new ImageIcon("table8_2.png");
	    imTable10_2=new ImageIcon("table10_2.png");
	    
	    tableImg2_1=imtable2_1.getImage();
		tableImg4_1=imTable4_1.getImage();
		tableImg6_1=imTable6_1.getImage();
		tableImg8_1=imTable8_1.getImage();
		tableImg10_1=imTable10_1.getImage();
		
		tableImg2_2=imtable2_2.getImage();
		tableImg4_2=imTable4_2.getImage();
		tableImg6_2=imTable6_2.getImage();
		tableImg8_2=imTable8_2.getImage();
		tableImg10_2=imTable10_2.getImage();
	}
	
	
	/**
	 * 
	 * @return vector of objects of class Table
	 */
	public Vector<Table> getVecTables() {
		return vecTables;
	}

/**
 * creates empty vector of tables
 */
	public void setVecTablesNull() {
		this.vecTables = new Vector<Table>();
		repaint();
	}
/**
 * sets vector of the tables
 * @param vecTables - vector of tables
 */
	public void setVecTables(Vector<Table> vecTables) {
		this.vecTables = vecTables;
		repaint();
	}
	
	
	public void setVecTablesAndImages(Vector<Table> vecTables){
		for(int i=0;i<vecTables.size();i++)
		{
		int size=vecTables.get(i).getSize();
		String type=vecTables.get(i).getType();
		if (size==2)
		{
			vecTables.get(i).setTable_1(setTableNumOnImage(tableImg2_1,Integer.toString(vecTables.get(i).getNumber())));
			vecTables.get(i).setTable_2(setTableNumOnImage(tableImg2_2,Integer.toString(vecTables.get(i).getNumber())));
			if(type.equals("horizontal"))
			vecTables.get(i).setTableToDisplay_horizontal();
			if(type.equals("vertical"))
				vecTables.get(i).setTableToDisplay_vertical();
			
		}
		
		if (size==4)
		{
			vecTables.get(i).setTable_1(setTableNumOnImage(tableImg4_1,Integer.toString(vecTables.get(i).getNumber())));
			vecTables.get(i).setTable_2(setTableNumOnImage(tableImg4_2,Integer.toString(vecTables.get(i).getNumber())));
			if(type.equals("horizontal"))
			vecTables.get(i).setTableToDisplay_horizontal();
			if(type.equals("vertical"))
				vecTables.get(i).setTableToDisplay_vertical();
			
		}
		
		if (size==6)
		{
			vecTables.get(i).setTable_1(setTableNumOnImage(tableImg6_1,Integer.toString(vecTables.get(i).getNumber())));
			vecTables.get(i).setTable_2(setTableNumOnImage(tableImg6_2,Integer.toString(vecTables.get(i).getNumber())));
			if(type.equals("horizontal"))
			vecTables.get(i).setTableToDisplay_horizontal();
			if(type.equals("vertical"))
				vecTables.get(i).setTableToDisplay_vertical();
			
		}
		
		if (size==8)
		{
			vecTables.get(i).setTable_1(setTableNumOnImage(tableImg8_1,Integer.toString(vecTables.get(i).getNumber())));
			vecTables.get(i).setTable_2(setTableNumOnImage(tableImg8_2,Integer.toString(vecTables.get(i).getNumber())));
			if(type.equals("horizontal"))
			vecTables.get(i).setTableToDisplay_horizontal();
			if(type.equals("vertical"))
				vecTables.get(i).setTableToDisplay_vertical();
			
		}
		
		if (size==10)
		{
			vecTables.get(i).setTable_1(setTableNumOnImage(tableImg10_1,Integer.toString(vecTables.get(i).getNumber())));
			vecTables.get(i).setTable_2(setTableNumOnImage(tableImg10_2,Integer.toString(vecTables.get(i).getNumber())));
			if(type.equals("horizontal"))
			vecTables.get(i).setTableToDisplay_horizontal();
			if(type.equals("vertical"))
				vecTables.get(i).setTableToDisplay_vertical();
			
		}
	}
		
		this.vecTables = vecTables;
		repaint();
	}
	
	
	/**
	 * this method sets the number of some table on the image of this table
	 * 
	 * @param table - image of a table
	 * @param num - number of a table
	 * @return - images with the number
	 */
	private Image setTableNumOnImage(Image table, String num)
	{
		MediaTracker mt = new MediaTracker(this);
        mt.addImage(table,0);
        try{mt.waitForID(0);}catch(InterruptedException ie){}
        int width = table.getWidth(null);
        int height = table.getHeight(null);
       
        BufferedImage bimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        bimg.createGraphics().drawImage(table, 0, 0, this.getBackground(), this);
        
        
        Graphics2D g2 = (Graphics2D) bimg.getGraphics();
		g2.setColor(Color.black);
		g2.setFont(f);
		g2.drawString(num,width/2,height/2);
  
        ImageIcon img = new ImageIcon(bimg);
        Image tableWithNumber=img.getImage();
        return tableWithNumber;
	}
	

	
//========================================================Listener for moving table ===========================================
	/**
	 * this methode creates the listener of moving image of table
	 */
	public void mouseDragged(MouseEvent arg0) 
    {
			int x = arg0.getX();
			int y = arg0.getY();
					
					
		for(int i=0;i<vecTables.size();i++)
		{
					if(x<=vecTables.get(i).getX()+60 && x>=vecTables.get(i).getX()-25 && y>=vecTables.get(i).getY()-25 && y<=vecTables.get(i).getY()+60 && vecTables.get(i).getNumber()==tableNumber )
					{   					
						vecTables.get(i).setX(x);					
						vecTables.get(i).setY(y);				
						repaint();					
						i=vecTables.size();
					}		
		}
    }
				

		
		
		
	public void mouseMoved(MouseEvent arg0) 
	{		

	}
	
	
	
	

	/**
	 * @param args
	 */
	
	
	
	
	
	
	
	
	
	
	


	
	
	
	
	
	
	
	
	
	/**
	 * this method creates the option "create new table" 
	 * the option will belong to JMenu 
	 * 
	 * @return JMenuItem- created option
	 */
	private JMenuItem  getJMenuItemNewTable()
	{
		JMenuItem newTable=new JMenuItem("יצירת שולחן חדש");
		newTable.addActionListener(new ActionListener(){
			  public void actionPerformed(ActionEvent e)
			  {
				  newTableFrame();
			  }
			  });
		
		
		return newTable;
	}
	
	
	/**
	 * this method creates the option "delete table" 
	 * the option will belong to JMenu 
	 * 
	 * @return JMenuItem- created option
	 */
	private JMenuItem  getJMenuItemRemoveTable()
	{
		JMenuItem newTable=new JMenuItem("מחיקת שולחן");
		newTable.addActionListener(new ActionListener(){
			  public void actionPerformed(ActionEvent e)
			  {
				  vecTables.remove(tableToRemove);
				  repaint();
			  }
			  });
		
		
		return newTable;
	}
	
	/**
	 * this method creates the option "update table" 
	 * the option will belong to JMenu 
	 * 
	 * @return JMenuItem- created option
	 */
	private JMenuItem  getJMenuItemChangeTable()
	{
		JMenuItem newTable=new JMenuItem("שינוי שולחן");
		newTable.addActionListener(new ActionListener(){
			  public void actionPerformed(ActionEvent e)
			  {
				 changeTableFrame();
			  }
			  });
		
		
		return newTable;
	}
	
	/**
	 * this method creates the option "turn table" 
	 * the option will belong to JMenu 
	 * 
	 * @return JMenuItem- created option
	 */
	private JMenuItem  getJMenuItemTurnTable()
	{
		JMenuItem newTable=new JMenuItem("סובב שוחן");
		newTable.addActionListener(new ActionListener(){
			  public void actionPerformed(ActionEvent e)
			  {
				 if(((String)vecTables.get(tableToTurn).getType()).equals("horizontal"))
				 {
					 vecTables.get(tableToTurn).setType_vertical();
					 vecTables.get(tableToTurn).setTableToDisplay_vertical();
					 repaint();
				 }
					 
				 else
				 {
					 vecTables.get(tableToTurn).setType_horizontal();
					 vecTables.get(tableToTurn).setTableToDisplay_horizontal();
					 repaint();
				 }
			  }
			  });
		
		
		return newTable;
	}
	
	
	
	
	/**
	 * creates the listener of mouse right click
	 * if right click was on table image, it will open the menu of options for this table
	 * @author igor
	 *
	 */
	private class RightClickerListener extends MouseAdapter
	{
		public void mouseReleased(MouseEvent Me)
		{
			int x=Me.getX();
			int y=Me.getY();
			
			
			
			  if(Me.isPopupTrigger())
			  {
				  if (x<=65 && x>=20 && y<=160 && y>=100)
				  {
			          Pmenu.show(Me.getComponent(), Me.getX(), Me.getY());
			          tableSize=2;
			          frameCoordX=Me.getX()+100;
			          frameCoordY=Me.getY();
			          
				  }
				  
				  if (x<=100 && x>=20 && y<=260 && y>=200)
				  {
			          Pmenu.show(Me.getComponent(), Me.getX(), Me.getY());
			          tableSize=4;
			          frameCoordX=Me.getX()+100;
			          frameCoordY=Me.getY();
			          
				  }
				  
				  if (x<=145 && x>=20 && y<=360 && y>=300)
				  {
			          Pmenu.show(Me.getComponent(), Me.getX(), Me.getY());
			          tableSize=6;
			          frameCoordX=Me.getX()+100;
			          frameCoordY=Me.getY();
			          
				  }
				  
				  if (x<=180 && x>=20 && y<=460 && y>=400)
				  {
			          Pmenu.show(Me.getComponent(), Me.getX(), Me.getY());
			          tableSize=8;
			          frameCoordX=Me.getX()+100;
			          frameCoordY=Me.getY();
			          
				  }
				  
				  if (x<=120 && x>=20 && y<=610 && y>=500)
				  {
			          Pmenu.show(Me.getComponent(), Me.getX(), Me.getY());
			          tableSize=10;
			          frameCoordX=Me.getX()+100;
			          frameCoordY=Me.getY();
			          
				  }
				  
				  for(int i=0;i<vecTables.size();i++)
					{
								if(x<=vecTables.get(i).getX()+60 && x>=vecTables.get(i).getX() && y>=vecTables.get(i).getY() && y<=vecTables.get(i).getY()+60  )
								{   					
									PmenuForMovedTables.show(Me.getComponent(), Me.getX(), Me.getY());
									tableToRemove=i;
									tableToTurn=i;
									tableForChange=vecTables.get(i);
									tableSize=vecTables.get(i).getSize();
							        frameCoordX=Me.getX()+100;
							        frameCoordY=Me.getY();
								}		
					}
				  
			  }
			  
			  
		
	    }
	}
	
	
	/**
	 * this method creates JFrame that appears then the option "create new table" was chosen
	 * this frame will set ip new table
	 * 
	 */
	private void newTableFrame()
	{
		String [] str={"2","4","6","8","10"};
		final JFrame f = new JFrame();
		f.setDefaultCloseOperation(f.DISPOSE_ON_CLOSE);
		f.setSize(500, 500);
		f.setLocation(frameCoordX, frameCoordY);
		f.pack();
		f.setVisible(true);
		
		JPanel panel= new JPanel();
		panel.setLayout(new SpringLayout());
		
	
		
		
		final JComboBox tableNo = new JComboBox(new ConstantObjects().getTableNo2());
		tableNo.setFont(new Font("Tahoma", Font.PLAIN, 13));
		//tableNo.setColumns(10);		
		JLabel tableNoLbl = new JLabel("מספר שולחן :", JLabel.TRAILING);
		tableNoLbl.setForeground(new Color(75, 0, 130));
		tableNoLbl.setFont(new Font("Tahoma", Font.BOLD, 12));
		tableNoLbl.setLabelFor(tableNo);	
		panel.add(tableNo);
		panel.add(tableNoLbl);
		
		final JComboBox combo= new JComboBox(str);
		combo.setSelectedItem(Integer.toString(tableSize));
		JLabel tablesizeLbl = new JLabel("גודל השולחן", JLabel.TRAILING);
		tablesizeLbl.setForeground(new Color(75, 0, 130));
		tablesizeLbl.setFont(new Font("Tahoma", Font.BOLD, 12));
		tablesizeLbl.setLabelFor(combo);	
		panel.add(combo);
		panel.add(tablesizeLbl);
		
		JPanel buttonsPanel=new JPanel();
		final JPanel errorPanel=new JPanel();
		JButton btnSave=new JButton("הוסף שולחן");
		btnSave.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {									
			
			if(isFree(Integer.parseInt((String) tableNo.getSelectedItem())))
			{
				Table table=new Table();
				int size=Integer.parseInt((String)combo.getSelectedItem());
				if (size==2)
				{
					table.setTable_1(setTableNumOnImage(tableImg2_1,(String) tableNo.getSelectedItem()));
					table.setTable_2(setTableNumOnImage(tableImg2_2,(String) tableNo.getSelectedItem()));
					table.setType_horizontal();
					table.setTableToDisplay_horizontal();
					
				}
				
				if (size==4)
				{
					table.setTable_1(setTableNumOnImage(tableImg4_1,(String) tableNo.getSelectedItem()));
					table.setTable_2(setTableNumOnImage(tableImg4_2,(String) tableNo.getSelectedItem()));
					table.setType_horizontal();
					table.setTableToDisplay_horizontal();
					
				}
				
				if (size==6)
				{
					table.setTable_1(setTableNumOnImage(tableImg6_1,(String) tableNo.getSelectedItem()));
					table.setTable_2(setTableNumOnImage(tableImg6_2,(String) tableNo.getSelectedItem()));
					table.setType_horizontal();
					table.setTableToDisplay_horizontal();
					
				}
				
				if (size==8)
				{
					table.setTable_1(setTableNumOnImage(tableImg8_1,(String) tableNo.getSelectedItem()));
					table.setTable_2(setTableNumOnImage(tableImg8_2,(String) tableNo.getSelectedItem()));
					table.setType_horizontal();
					table.setTableToDisplay_horizontal();
					
				}
				
				if (size==10)
				{
					table.setTable_1(setTableNumOnImage(tableImg10_1,(String) tableNo.getSelectedItem()));
					table.setTable_2(setTableNumOnImage(tableImg10_2,(String) tableNo.getSelectedItem()));
					table.setType_horizontal();
					table.setTableToDisplay_horizontal();
					
				}
				
				table.setSize(size);
				table.setX(frameCoordX);
				table.setY(frameCoordY);
				table.setNumber(Integer.parseInt((String) tableNo.getSelectedItem()));
				vecTables.add(table);
				f.dispose();
				repaint();
			}                            //if(isFree(Integer.parseInt((String) tableNo.getSelectedItem())));
			
			else
			{
				errorPanel.removeAll();
				JLabel errorLbl=new JLabel(".מספר שולחן קיים. נא לבחור מספר אחר");
				errorLbl.setForeground(Color.red);
				errorPanel.add(errorLbl);
				f.getContentPane().validate();
			}
			}
		});
		
		
		
		SpringUtilities.makeCompactGrid(panel, 2, 2, //rows, cols
			    100, 5, //initX, initY
			        80, 10); //xPad, yPad
		
		
		
		
		
		
		buttonsPanel.add(btnSave);
		
		JPanel framePanel=new JPanel();
		JPanel buttAndErrPanel=new JPanel();
		buttAndErrPanel.setLayout(new BoxLayout(buttAndErrPanel, BoxLayout.Y_AXIS));
		framePanel.setLayout(new BoxLayout(framePanel, BoxLayout.Y_AXIS));
		
		buttAndErrPanel.add(errorPanel);
		buttAndErrPanel.add(buttonsPanel);
		
		
		framePanel.add(panel);
		framePanel.add(buttAndErrPanel);
		
		
		
		f.add(framePanel);
		f.pack();
		f.setVisible(true);
		
	}
	
	
	
	
	
	
	/**
	 * this method creates JFrame that appears then the option "update table" was chosen
	 * this frame will update the chosen table
	 * 
	 */
	
	private void changeTableFrame()
	{
		String [] str={"2","4","6","8","10"};
		final JFrame f = new JFrame();
		f.setDefaultCloseOperation(f.DISPOSE_ON_CLOSE);
		f.setSize(500, 500);
		f.setLocation(frameCoordX, frameCoordY);
		f.pack();
		f.setVisible(true);
		
		JPanel panel= new JPanel();
		panel.setLayout(new SpringLayout());
		
	
		
		
		final JComboBox tableNo = new JComboBox(new ConstantObjects().getTableNo2());
		tableNo.setSelectedItem(Integer.toString(tableForChange.getNumber()));
		tableNo.setFont(new Font("Tahoma", Font.PLAIN, 13));		
		JLabel tableNoLbl = new JLabel("מספר שולחן :", JLabel.TRAILING);
		tableNoLbl.setForeground(new Color(75, 0, 130));
		tableNoLbl.setFont(new Font("Tahoma", Font.BOLD, 12));
		tableNoLbl.setLabelFor(tableNo);	
		panel.add(tableNo);
		panel.add(tableNoLbl);
		
		final JComboBox combo= new JComboBox(str);
		combo.setSelectedItem(Integer.toString(tableSize));
		JLabel tablesizeLbl = new JLabel("גודל השולחן", JLabel.TRAILING);
		tablesizeLbl.setForeground(new Color(75, 0, 130));
		tablesizeLbl.setFont(new Font("Tahoma", Font.BOLD, 12));
		tablesizeLbl.setLabelFor(combo);	
		panel.add(combo);
		panel.add(tablesizeLbl);
		
		JPanel buttonsPanel=new JPanel();
		final JPanel errorPanel=new JPanel();
		JButton btnSave=new JButton("שמור");
		btnSave.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {									
			
			if(isFree(tableForChange.getNumber()))
			{
				int size=Integer.parseInt((String)combo.getSelectedItem());
				if (size==2)
				{
					tableForChange.setTable_1(setTableNumOnImage(tableImg2_1,(String) tableNo.getSelectedItem()));
					tableForChange.setTable_2(setTableNumOnImage(tableImg2_2,(String) tableNo.getSelectedItem()));
					if(((String)tableForChange.getType()).equals("horizontal"))
					tableForChange.setTableToDisplay_horizontal();
					
					
				}
				
				if (size==4)
				{
					tableForChange.setTable_1(setTableNumOnImage(tableImg4_1,(String) tableNo.getSelectedItem()));
					tableForChange.setTable_2(setTableNumOnImage(tableImg4_2,(String) tableNo.getSelectedItem()));
					if(((String)tableForChange.getType()).equals("horizontal"))
					tableForChange.setTableToDisplay_horizontal();
					
				}
				
				if (size==6)
				{
					tableForChange.setTable_1(setTableNumOnImage(tableImg6_1,(String) tableNo.getSelectedItem()));
					tableForChange.setTable_2(setTableNumOnImage(tableImg6_2,(String) tableNo.getSelectedItem()));
					if(((String)tableForChange.getType()).equals("horizontal"))
					tableForChange.setTableToDisplay_horizontal();
					
				}
				
				if (size==8)
				{
					tableForChange.setTable_1(setTableNumOnImage(tableImg8_1,(String) tableNo.getSelectedItem()));
					tableForChange.setTable_2(setTableNumOnImage(tableImg8_2,(String) tableNo.getSelectedItem()));
					if(((String)tableForChange.getType()).equals("horizontal"))
					tableForChange.setTableToDisplay_horizontal();
					
				}
				
				if (size==10)
				{
					tableForChange.setTable_1(setTableNumOnImage(tableImg10_1,(String) tableNo.getSelectedItem()));
					tableForChange.setTable_2(setTableNumOnImage(tableImg10_2,(String) tableNo.getSelectedItem()));
					if(((String)tableForChange.getType()).equals("horizontal"))
					tableForChange.setTableToDisplay_horizontal();
					
				}
				
				tableForChange.setSize(size);
				tableForChange.setNumber(Integer.parseInt((String) tableNo.getSelectedItem()));
				f.dispose();
				repaint();
			}                        //if(isFree(tableForChange.getNumber()))
				
				else
				{
					errorPanel.removeAll();
					JLabel errorLbl=new JLabel(".מספר שולחן קיים. נא לבחור מספר אחר");
					errorLbl.setForeground(Color.red);
					errorPanel.add(errorLbl);
					f.getContentPane().validate();
					
				}
			}
		});
		
		
		
		SpringUtilities.makeCompactGrid(panel, 2, 2, //rows, cols
			     100, 5, //initX, initY
			        80, 10); //xPad, yPad
		
		
		
		
		
		
		buttonsPanel.add(btnSave);
		
		
		JPanel framePanel=new JPanel();
		JPanel buttAndErrPanel=new JPanel();
		buttAndErrPanel.setLayout(new BoxLayout(buttAndErrPanel, BoxLayout.Y_AXIS));
		framePanel.setLayout(new BoxLayout(framePanel, BoxLayout.Y_AXIS));
		
		buttAndErrPanel.add(errorPanel);
		buttAndErrPanel.add(buttonsPanel);
		
		framePanel.add(panel);
		framePanel.add(buttAndErrPanel);
		
		
		
		f.add(framePanel);
		f.pack();
		f.setVisible(true);
		
	}
	
	
	private boolean isFree(int num)
	{
		for(int i=0; i<vecTables.size();i++)
		{
			if (vecTables.get(i).getNumber()==num)  return false;
		}
		
		return true;
	}
	
	
	
	
	
	
	
	
	
	private class MyJPopupMenu extends JPopupMenu
	{
		
		MyJPopupMenu()
		{
			super();
		}
		
		
	}


	@Override
	public void mouseClicked(MouseEvent Me) {
		if (Me.getClickCount() == 2){
			int x=Me.getX();
			int y=Me.getY();
			
			
			///
			 //(Me.isPopupTrigger())
			//  {
				  if (x<=65 && x>=20 && y<=160 && y>=100)
				  {
			          Pmenu.show(Me.getComponent(), Me.getX(), Me.getY());
			          tableSize=2;
			          frameCoordX=Me.getX()+100;
			          frameCoordY=Me.getY();
			          
				  }
				  
				  if (x<=100 && x>=20 && y<=260 && y>=200)
				  {
			          Pmenu.show(Me.getComponent(), Me.getX(), Me.getY());
			          tableSize=4;
			          frameCoordX=Me.getX()+100;
			          frameCoordY=Me.getY();
			          
				  }
				  
				  if (x<=145 && x>=20 && y<=360 && y>=300)
				  {
			          Pmenu.show(Me.getComponent(), Me.getX(), Me.getY());
			          tableSize=6;
			          frameCoordX=Me.getX()+100;
			          frameCoordY=Me.getY();
			          
				  }
				  
				  if (x<=180 && x>=20 && y<=460 && y>=400)
				  {
			          Pmenu.show(Me.getComponent(), Me.getX(), Me.getY());
			          tableSize=8;
			          frameCoordX=Me.getX()+100;
			          frameCoordY=Me.getY();
			          
				  }
				  
				  if (x<=120 && x>=20 && y<=610 && y>=500)
				  {
			          Pmenu.show(Me.getComponent(), Me.getX(), Me.getY());
			          tableSize=10;
			          frameCoordX=Me.getX()+100;
			          frameCoordY=Me.getY();
			          
				  }
				  
				  for(int i=0;i<vecTables.size();i++)
					{
								if(x<=vecTables.get(i).getX()+60 && x>=vecTables.get(i).getX() && y>=vecTables.get(i).getY() && y<=vecTables.get(i).getY()+60  )
								{   					
									PmenuForMovedTables.show(Me.getComponent(), Me.getX(), Me.getY());
									tableToRemove=i;
									tableToTurn=i;
									tableForChange=vecTables.get(i);
									tableSize=vecTables.get(i).getSize();
							        frameCoordX=Me.getX()+100;
							        frameCoordY=Me.getY();
								}		
					}
				  
			 // }
	         }
	}




	@Override
	public void mouseEntered(MouseEvent arg0) {

		
	}




	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}




	@Override
	public void mousePressed(MouseEvent arg0) {
		int x = arg0.getX();
		int y = arg0.getY();		
	
        for(int i=0;i<vecTables.size();i++)
        {    
			if(x<=vecTables.get(i).getX()+40 && x>=vecTables.get(i).getX() && y>=vecTables.get(i).getY() && y<=vecTables.get(i).getY()+50  )
			{   
				tableNumber=vecTables.get(i).getNumber();								
				i=vecTables.size();
			}		
       }
		
	}




	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	
	
/**
 * repaint the panel
 */
	public void paintComponent( Graphics g )
	{
		int tempQuantity2=quantity2;
		int tempQuantity4=quantity4;
		super.paintComponent(g);			
		dim=this.getSize();
		g.drawImage(background.getImage(), 0, 0, dim.width,dim.height, this);
		g.drawImage(tableImg2_1,table2X,table2Y, null);
		g.drawImage(tableImg4_1,table4X, table4Y, null);
		g.drawImage(tableImg6_1,table6X,table6Y, null);
		g.drawImage(tableImg8_1,table8X, table8Y, null);
		g.drawImage(tableImg10_1,table10X, table10Y, null);
		for(int i=0;i<vecTables.size();i++)
		{			
			g.drawImage(vecTables.get(i).getTable(),vecTables.get(i).getX(),vecTables.get(i).getY(), null);
			if(vecTables.get(i).getSize()==2) tempQuantity2--;
			if(vecTables.get(i).getSize()==4) tempQuantity4--;
			if(vecTables.get(i).getSize()==6) 
				{ 
				tempQuantity4=tempQuantity4-1;
				tempQuantity2=tempQuantity2-1;
				}
			if(vecTables.get(i).getSize()==8 || vecTables.get(i).getSize()==10) 
			{ 
				tempQuantity4=tempQuantity4-2;
			}
			
		
				
		}//for(int i=0;i<vecTables.size();i++)
		String str2=("נותרו :"+tempQuantity2);
		String str4=("נותרו :"+tempQuantity4);
		g.drawString(str2, 25, 170);
		g.drawString(str4, 25, 270);
	}

}
